package com.pinc.poop;

import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class statistic extends PwdProtectWidgetActivity{
	private DBClass dbHelper;
	private int[] state;
	private int[] chart_height;
	private TextView[] tv_value;
	private LinearLayout[] iv_chart;
	private TextView[] tv_foot;
	private int total;
	private int max_chart_height;
	private PincDate g_date;
	private PincDate today;
	private boolean isViewAll;
	private static final int interval_hours = 4;
	private static final int startHour = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistics);
		state = new int[24/interval_hours];
		chart_height = new int[24/interval_hours];
		dbHelper = new DBClass(this);
		today = new PincDate();
		g_date = new PincDate();
		g_date.setToNow();
		isViewAll = true;
		initWidget();
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		tv_left.setImageDrawable(getResources().getDrawable(R.drawable.back));
		tv_left.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadAD();
				if ( isViewAll)
					g_date.setToNow();
				else
					g_date.nextMonth(-1);
				isViewAll = false;
				new ShowUITask().execute(getApplicationContext());
			}
		});

		tv_right.setImageDrawable(getResources().getDrawable(R.drawable.next));
		tv_right.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadAD();
				if ( isViewAll)
					return;
				today.setToNow();
				if ( g_date.month == today.month)
					isViewAll = true;
				else
					g_date.nextMonth(1);
				new ShowUITask().execute(getApplicationContext());
			}
		});

		tv_value = new TextView[24/interval_hours];
		tv_foot = new TextView[24/interval_hours];
		iv_chart = new LinearLayout[24/interval_hours];

		Resources res = getResources();
		String pkgname = getPackageName();
		for (int i = 0; i < 24/interval_hours; i++) {
			int resID = res.getIdentifier(String.format("tv_value%d", i+1), "id", pkgname);
			tv_value[i] = (TextView) findViewById(resID);
			resID = res.getIdentifier(String.format("imageView_chart%d", i+1), "id", pkgname);
			iv_chart[i] = (LinearLayout) findViewById(resID);
			resID = res.getIdentifier(String.format("tv_foot%d", i+1), "id", pkgname);
			tv_foot[i] = (TextView) findViewById(resID);
			tv_foot[i].setText(String.format("%02d-%02d", startHour+i*interval_hours, (startHour+(i+1)*interval_hours)%24));
		}
	}

	@Override
	protected void onValidResume() {
		Display display = getWindowManager().getDefaultDisplay(); 
		max_chart_height = (int) (display.getHeight() * 0.55);
		new ShowUITask().execute(getApplicationContext());
	}

	private void updateTitle()
	{
		String rangeStr = "("+((isViewAll)?getResources().getString(R.string.str_all): String.format("%s, %d", getResources().getStringArray(R.array.monthName)[g_date.month], g_date.year))+")"; 
		tv_title.setText(String.format("%s %s", getResources().getString(R.string.str_statistics), rangeStr));
	}

	private static int getChartHeight(int times, int total, int height)
	{
		if ( total == 0)
			return 1;
		float ret = (float)(times) / (float)total * (float)height;
		return (int)(ret+1);
	}

	private ImageView createChart(int height)
	{
		ImageView iv = new ImageView(this);
		iv.setBackgroundResource(R.drawable.chart_bg);
		iv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, height));
		return iv;
	}

	private class ShowUITask extends AsyncTask<Context, Void, Integer> {
		protected Integer doInBackground(Context... ctx) {
			total = 0;
			for ( int i=startHour; i<startHour+24; i+=interval_hours)
			{
				state[i/interval_hours]= dbHelper.getRecordCount("date LIKE ? AND (time LIKE ? OR time LIKE ? OR time LIKE ? OR time LIKE ?)", new String[]{(isViewAll)?"%":g_date.format("%Y%m%%"), String.format("%02d:%%", i%24), String.format("%02d:%%", (i+1)%24), String.format("%02d:%%", (i+2)%24), String.format("%02d:%%", (i+3)%24)});
				total += state[i/interval_hours];
			}
			for (int i = 0; i < 24/interval_hours; i++)
			{
				chart_height[i]= getChartHeight(state[i], total, max_chart_height);
			}
			return 0;
		}

		@Override
		protected void onPreExecute() {
			tv_left.setEnabled(false);
			tv_right.setEnabled(false);
			updateTitle();
			for (int i = 0; i < 24/interval_hours; i++)
			{
				tv_value[i].setVisibility(View.GONE);
				iv_chart[i].removeAllViews();
			}
		}

		@Override
		protected void onPostExecute(Integer err) {
			for (int i = 0; i < 24/interval_hours; i++)
			{
				tv_value[i].setText(String.valueOf(state[i]));
				iv_chart[i].addView(createChart(chart_height[i]));
				Animation anim = AnimationUtils.loadAnimation(statistic.this, R.anim.chart_grow);
				anim.setAnimationListener(new mAnimationListener(tv_value[i]));
				iv_chart[i].startAnimation(anim);
			}
		}
	}

	private class mAnimationListener implements Animation.AnimationListener
	{
		private TextView tv;

		public mAnimationListener(TextView tv) {
			this.tv = tv;
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			tv.setVisibility(View.VISIBLE);
			tv.startAnimation(AnimationUtils.loadAnimation(statistic.this, R.anim.fade_in));
			tv_left.setEnabled(true);
			tv_right.setEnabled(true);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
		}

		@Override
		public void onAnimationStart(Animation animation) {
		}
	}

	//	@Override
	//	protected void onCreate(Bundle savedInstanceState) {
	//		super.onCreate(savedInstanceState);
	//		setContentView(R.layout.statistic);
	//		state = new int[24/interval_hours];
	//		nll = new chart_unit[24/interval_hours];
	//		initWidget();
	//	}
	//
	//	@Override
	//	protected void initWidget() {
	//		super.initWidget();
	//
	//		tv_right.setImageDrawable(getResources().getDrawable(R.drawable.back));
	//		tv_right.setOnClickListener(new View.OnClickListener() {
	//			@Override
	//			public void onClick(View v) {
	//				new ShowUITask().execute(getApplicationContext());
	//			}
	//		});
	//
	//		tv_left.setVisibility(View.INVISIBLE);
	//
	//		tv_title.setText(getResources().getString(R.string.str_statistics));
	//
	//		ll = (LinearLayout) this.findViewById(R.id.linearLayout1);
	//	}
	//
	//	@Override
	//	protected void onValidResume() {
	//		dbHelper = new DBClass(this);
	//		new ShowUITask().execute(getApplicationContext());
	//	}
	//
	//	private class ShowUITask extends AsyncTask<Context, Void, Integer> {
	//		protected Integer doInBackground(Context... ctx) {
	//			int total = 0;
	//			for ( int i=startHour; i<startHour+24; i+=interval_hours)
	//			{
	//				state[i/interval_hours]= dbHelper.getRecordCount("time LIKE ? OR time LIKE ? OR time LIKE ? OR time LIKE ?", new String[]{String.format("%02d:%%", i%24), String.format("%02d:%%", (i+1)%24), String.format("%02d:%%", (i+2)%24), String.format("%02d:%%", (i+3)%24)});
	//				total += state[i/interval_hours];
	//			}
	//			for ( int i=startHour; i<startHour+24; i+=interval_hours)
	//			{
	//				nll[i/interval_hours] = new chart_unit(statistic.this);
	//				nll[i/interval_hours].setChart(state[i/interval_hours], total, i,(i+interval_hours)%24);
	//			}
	//			return 0;
	//		}
	//
	//		@Override
	//		protected void onPreExecute() {
	//			ll.removeAllViews();
	//		}
	//
	//		@Override
	//		protected void onPostExecute(Integer err) {
	//			for ( int i=startHour; i<startHour+24; i+=interval_hours)
	//			{
	//				ll.addView(nll[i/interval_hours]);
	//				nll[i/interval_hours].startAnimation(AnimationUtils.loadAnimation(statistic.this, R.anim.chart_grow));
	//			}
	//		}
	//	}

}
